<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/23 0023
 * Time: 11:07
 */
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends CommonController {
    function __construct()
    {
        parent::__construct();
        //查出所有分类
        $Article = D('Article');
        $this->categories = $Article->all_categories();
    }


    /***
     * 文章管理--首页
     */
    public function index()
    {
        $Article = D('Article');
//        $articles = $Article->all_articles();

        $articles = $Article->relation(true)->where('type=1')->order('sort_order desc')->select();

        $this->assign('articles', $articles);
        $this->display();
    }
    /***
     * 文章管理--新增
     */
    public function add()
    {
//        $Article = D('Article');
//        if (IS_POST) {
//            $Article->create();
//            $Article->add();
//            $this->success('新增文章成功', U('index'));
//        } else {
//            $this->display();
//        }

        if (IS_POST) {
            $Article = M('Article');
            $time = I('post.time');
            $Article->create();
            $Article->time = strtotime($time);
            $Article->add();
            $this->success('新增成功', U('index'));
        } else {
            $this->display();
        }
    }

    /***
     * 文章管理--编辑
     */
    public function edit(){
        $id = I('get.id');
        if(IS_POST){
            $Article = M('Article');
            $articles = $this->article->find($id);
            $this->assign('articles',$articles);
            $Article->where("id = '$id'")->save();
            $this->success('恭喜你，文章编辑成功',U('index'));
        }else{
            $this->display();
        }
//        $id = I('get.id');
//        $cate = $this->category->find($id);
//        $this->assign('cate', $cate);
//        $this->display();

    }


    /***
     * 文章管理--删除
     */
    public function delete(){
        $id = I('get.id');
//        $Article = M('Article');
        $this->article->where("id = '$id'")->setFieLd('type',0);
        $this->success('文章放入回收站成功',U('trash'));

    }

    /***
     * --加载文章回收站
     */
    public function trash()
    {
        $this->articles = $this->article->where('type = 0')->select();
        $this->display();
    }
    /***
     * 回收站彻底删除
     */
    public function confirm_delete(){
        $id = I('get.id');
        $this->article->delete($id);
        $this->success('文章彻底删除成功',U('index'));
    }

    /***
     * 回收站文章恢复
     */
    public function retrash(){
        $id = I('get.id');
        $this->article->where("id = '$id'")->setField('type',1);
        $this->success('文章恢复成功',U('index'));
    }

}
